package com.bss.command;

import java.util.ArrayList;
import java.util.Date;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.bss.beans.Attendent;
import com.bss.beans.CheckInOut;
import com.bss.beans.MaterialAllocation;
import com.bss.beans.Patient;
import com.bss.beans.RoomAllocation;
import com.bss.beans.UtensilAllocation;
import com.bss.dao.CheckInOutDao;
import com.bss.dao.PatientRecordDao;
import com.bss.dao.RoomDao;

public class CheckInOutCommand extends AbstractCommand {

	@Override
	public boolean execute(HttpServletRequest request, HttpSession session)
			throws Exception {
		// TODO Auto-generated method stub
		
		if(Integer.parseInt((session.getAttribute("action")).toString())==1)
		{
			 if(checkin(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==2)
		{
			 if(addAttendent(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==3)
		{
			 if(checkout(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}else if(Integer.parseInt((session.getAttribute("action")).toString())==4)
		{
			 if(allocateMaterial(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}else if(Integer.parseInt((session.getAttribute("action")).toString())==5)
		{
			 if(getMaterials(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==6)
		{
			 if(returnMaterials(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==7)
		{
			 if(allocateNewUtensils(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==8)
		{
			 if(returnUtensils(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==9)
		{
			System.out.println("Checking in...");
			 if(checkinmain(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==10)
		{
			 if(checkoutmain(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==11)
		{
			 if(findattendentstaylist(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==12)
		{
			 if(updateattendentstaylist(request, session)==false)
			{
				return false;
			}
			else
			{
				session.removeAttribute("Error");
				return true;
			}
		}
		return false;
	}

	@Override
	public boolean falseforward(HttpServletResponse response,
			HttpSession session) throws Exception {
		// TODO Auto-generated method stub
		if(Integer.parseInt((session.getAttribute("action")).toString())==1)
		{
			session.setAttribute("welcomemsg", "UNABLE TO CHECK IN PATIENT");
			response.sendRedirect("checkinout.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==2)
		{
			session.setAttribute("welcomemsg", "UNABLE TO ADD ATTENDENTS");
			response.sendRedirect("checkinout.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==3)
		{
			if(session.getAttribute("Error")==null)
			{
			session.setAttribute("welcomemsg", "UNABLE TO CheckOutPatient");
			response.sendRedirect("checkinout.jsp");
			}
			else
			{
				response.sendRedirect("CHECKINOUT/checkout.jsp");
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==4)
		{
			if(session.getAttribute("Error")==null)
			{
				session.setAttribute("welcomemsg", "UNABLE TO ALLOCATE MATERIALS");
				response.sendRedirect("checkinout.jsp");
			}
			else
			{
				response.sendRedirect("CHECKINOUT/materialallocation.jsp");
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==5)
		{
			if(session.getAttribute("Error")==null)
			{
			session.setAttribute("welcomemsg", "UNABLE TO SHOW ALLOCATED ITEMS");
			response.sendRedirect("checkinout.jsp");
			}
			else
			{
				response.sendRedirect("CHECKINOUT/returnMaterials.jsp");
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==6)
		{
			session.setAttribute("welcomemsg", "UNABLE TO PROCESS RETURN OF MATERIALS");
			response.sendRedirect("checkinout.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==7)
		{
			if(session.getAttribute("Error")==null)
			{
			session.setAttribute("welcomemsg", "UNABLE TO ALLOCATE UTENSILS");
			response.sendRedirect("checkinout.jsp");
			}
			else
			{
				response.sendRedirect("CHECKINOUT/utensilallocation.jsp");
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==8)
		{
			if(session.getAttribute("Error")==null)
			{
			session.setAttribute("welcomemsg", "UNABLE TO DEALLOCATE UTENSILS");
			response.sendRedirect("checkinout.jsp");
			}
			else
			{
				response.sendRedirect("CHECKINOUT/deallocateutensils.jsp");
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==9)
		{
			if(session.getAttribute("Error")==null)
			{
				session.setAttribute("welcomemsg", "UNABLE TO CHECK IN PATIENT");
				response.sendRedirect("checkinout.jsp");
			}
			else
			{
				//System.out.print("returning false checkin main:"+session.getAttribute("Error").toString());
				response.sendRedirect("CHECKINOUT/checkinmain.jsp");
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==10)
		{
			if(session.getAttribute("Error")==null)
			{
			session.setAttribute("welcomemsg", "UNABLE TO CHECK OUT PATIENT");
			response.sendRedirect("checkinout.jsp");
			}
			else
			{
				response.sendRedirect("CHECKINOUT/checkoutmain.jsp");
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==11)
		{
			if(session.getAttribute("Error")==null)
			{
			session.setAttribute("welcomemsg", "UNABLE TO GENERATE ATTENDENT LIST");
			response.sendRedirect("checkinout.jsp");
			}
			else
			{
				response.sendRedirect("CHECKINOUT/updateattendentstay.jsp");
			}
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==12)
		{
			if(session.getAttribute("Error")==null)
			{
				session.setAttribute("welcomemsg", "UNABLE TO UPDATE ATTENDENT STATUS OF STAY");
				response.sendRedirect("checkinout.jsp");
			}
			else
			{
				response.sendRedirect("CHECKINOUT/updateattendentstaylist.jsp");
			}
		}
		return false;
	}

	@Override
	public boolean trueforward(HttpServletResponse response, HttpSession session)
			throws Exception {
		// TODO Auto-generated method stub
		if(Integer.parseInt((session.getAttribute("action")).toString())==1)
		{
			session.setAttribute("welcomemsg", "SUCESSFULLY CHECKED IN PATIENT");
			response.sendRedirect("checkinout.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==2)
		{
			session.setAttribute("welcomemsg", "SUCESSFULLY ADDED ATTENDENT");
			response.sendRedirect("checkinout.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==3)
		{
			session.setAttribute("welcomemsg", "CHECKOUT PATIENT SUCESSFULL");
			response.sendRedirect("checkinout.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==4)
		{
			session.setAttribute("welcomemsg", "MATERIALS ALLOCATED");
			response.sendRedirect("checkinout.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==5)
		{
			session.setAttribute("welcomemsg", "MATERIALS ALLOCATED");
			response.sendRedirect("CHECKINOUT/allocatedMaterial.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==6)
		{
			session.setAttribute("welcomemsg", "SUCESSFULLY PROCESSED RETURN OF MATERIALS");
			response.sendRedirect("checkinout.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==7)
		{
			session.setAttribute("welcomemsg", "SUCESSFULLY ALLOCATED UTENSILS");
			response.sendRedirect("checkinout.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==8)
		{
			int amount=Integer.parseInt(session.getAttribute("utensil_amount").toString());
			String str="SUCESSFULLY DEALLOCATED UTENSILS PLEASE REIMBURESE AMOUNT:Rs." + amount+ "Only" ;
			session.setAttribute("welcomemsg", str);
			response.sendRedirect("checkinout.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==9)
		{
			String str="SUCESSFULLY CHECKED IN PATIENT <br> <a href='CHECKINOUT/generateidcard.jsp?patientId="+session.getAttribute("patientId").toString()+"'>Generate ID CARD</a>" ;
			session.setAttribute("welcomemsg", str);
			response.sendRedirect("checkinout.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==10)
		{
			String str="SUCESSFULLY CHECKED OUT PATIENT" ;
			session.setAttribute("welcomemsg", str);
			response.sendRedirect("checkinout.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==11)
		{
			String str="SUCESSFULLY CHECKED OUT PATIENT" ;
			session.setAttribute("welcomemsg", str);
			response.sendRedirect("CHECKINOUT/updateattendentstaylist.jsp");
		}
		else if(Integer.parseInt((session.getAttribute("action")).toString())==12)
		{
			String str="SUCESSFULLY SAVED THE STATUS OF PATIENTS" ;
			session.setAttribute("welcomemsg", str);
			response.sendRedirect("checkinout.jsp");
		}
		return false;
	}
	
	private boolean checkin(HttpServletRequest request, HttpSession session) throws Exception
	{
		CheckInOut checkin=new CheckInOut();
		session.removeAttribute("Error");
		
		
		checkin.setIPatientId(Integer.parseInt(request.getParameter("patientId")));
		checkin.setINoOfChild(Integer.parseInt(request.getParameter("children")));
		checkin.setINoOfFemale(Integer.parseInt(request.getParameter("female")));
		checkin.setINoOfMale(Integer.parseInt(request.getParameter("male")));
		checkin.setCalCheckInTime(request.getParameter("date").toString());
		checkin.setSDateOfStay(request.getParameter("date1").toString());
		checkin.setSExtDateOfStay(request.getParameter("date2").toString());
		checkin.setSLastDepTown(request.getParameter("town").toString());
		checkin.setIDepositAmount(Integer.parseInt(request.getParameter("amount")));
		
		CheckInOutDao.checkin(checkin);
		
		return true;
	}

	private boolean addAttendent(HttpServletRequest request, HttpSession session) throws Exception
	{
		Attendent attendent=new Attendent();
		session.removeAttribute("Error");
		
		
		attendent.setIPatientId(Integer.parseInt(request.getParameter("patientId")));
		attendent.setITransactionId(CheckInOutDao.findLastTransactionId(attendent.getIPatientId()));
		attendent.setSName(request.getParameter("name").toString());
		attendent.setSRelationship(request.getParameter("relationship").toString());
		attendent.setIAttendentUId(CheckInOutDao.FindMaxAttendentId()+1);
		attendent.setSIdType(request.getParameter("idType").toString());
		attendent.setSIdNo(request.getParameter("idNo").toString());
		CheckInOutDao.AddNewAttendent(attendent);
		
		if(request.getParameter("updatememcnt")!=null)
		{
			com.bss.framework.FrontController.trace("...............changing the number");
			ArrayList<Integer> count = new ArrayList<Integer>();
			count.add(Integer.parseInt(request.getParameter("noofmale")));
			count.add(Integer.parseInt(request.getParameter("nooffemale")));
			count.add(Integer.parseInt(request.getParameter("noofchild")));
			CheckInOutDao.setAttendentCount(attendent.getITransactionId(), count);
		}
		
		return true;
	}
	
	
	public static ArrayList<Integer> loadCheckedInPatientID() throws Exception
	{
		return CheckInOutDao.loadCheckedInPatientId();
	}
	
	private boolean checkout(HttpServletRequest request, HttpSession session) throws Exception
	{
		session.removeAttribute("Error");
		int patientId=Integer.parseInt(request.getParameter("patientId"));
		int transactionId=CheckInOutCommand.findLastTransactionId(patientId);
		if(CheckInOutDao.canPatientCheckOut(patientId)==false)
		{
			session.setAttribute("Error", "User Cannot Checkout:Please return the Materials");
			return false;
		}
		
		String date=request.getParameter("date").toString();
		RoomDao.deallocateAllRooms(transactionId, date);
		CheckInOutDao.setCheckoutDate(date, patientId);
		
		
		
		return true;
	}
	
	private boolean allocateMaterial(HttpServletRequest request, HttpSession session) throws Exception
	{
		session.removeAttribute("Error");
		
		if(request.getParameter("patientId") != null)
		{
			if(request.getParameter("patientId").toString().length() != 0)
			{
				if(CheckInOutDao.canPatAllocMat(CheckInOutCommand.findLastTransactionId(Integer.parseInt(request.getParameter("patientId").toString()))) == false)
				{
					session.setAttribute("Error", "Patient Already Allocated Utensils");
					return false;
				}
			}
			else if(session.getAttribute("patientId") != null)
			{
				if(CheckInOutDao.canPatAllocMat(CheckInOutCommand.findLastTransactionId(Integer.parseInt(session.getAttribute("patientId").toString()))) == false)
				{
					session.setAttribute("Error", "Patient Already Allocated Utensils");
					return false;
				}
			}
		}
		else if(session.getAttribute("patientId") != null)
		{
			if(CheckInOutDao.canPatAllocMat(CheckInOutCommand.findLastTransactionId(Integer.parseInt(session.getAttribute("patientId").toString()))) == false)
					
			{
				session.setAttribute("Error", "Patient Already Allocated Utensils");
				return false;
			}
		}
		
				
		MaterialAllocation materials=new MaterialAllocation();
		
		materials.setIAllocationId(CheckInOutDao.FindMaxMaterialAllocationId()+1);
		materials.setINoOfBS(Integer.parseInt(request.getParameter("bs")));
		materials.setINoOfWBS(Integer.parseInt(request.getParameter("dbs")));
		materials.setINoOfMN(Integer.parseInt(request.getParameter("mn")));
		materials.setINoOfDMN(Integer.parseInt(request.getParameter("dmn")));
		materials.setINoOfLK(Integer.parseInt(request.getParameter("lk")));
		materials.setINoOfMG(Integer.parseInt(request.getParameter("mg")));
		materials.setINoOfBK(Integer.parseInt(request.getParameter("bkt")));
		materials.setINoOfMlkTkt(Integer.parseInt(request.getParameter("mlktkt")));
		materials.setIOthersVal1(Integer.parseInt(request.getParameter("othersvalue1")));
		materials.setIOthersVal2(Integer.parseInt(request.getParameter("othersvalue2")));
		materials.setSOthersType1((request.getParameter("otherstype1")).toString());
		materials.setSOthersType2((request.getParameter("otherstype2")).toString());
		materials.setReturned(false);

		CheckInOutDao.AllocateMaterial(materials);
		if(request.getParameter("patientId") != null)
		{
			if(request.getParameter("patientId").toString().length() != 0)
			{
				CheckInOutDao.setMaterials(materials.getIAllocationId(), Integer.parseInt(request.getParameter("patientId")));
			}
			else if(session.getAttribute("patientId") != null)
			{
				CheckInOutDao.setMaterials(materials.getIAllocationId(), Integer.parseInt(session.getAttribute("patientId").toString()));
			}
		}
		else if(session.getAttribute("patientId") != null)
		{
			CheckInOutDao.setMaterials(materials.getIAllocationId(), Integer.parseInt(session.getAttribute("patientId").toString()));
		}
		
		return true;
	}
	
	private boolean getMaterials(HttpServletRequest request, HttpSession session) throws Exception
	{
		session.removeAttribute("Error");
		MaterialAllocation materials=new MaterialAllocation();
		if(CheckInOutDao.selectMaterialsAllocated(materials, Integer.parseInt(request.getParameter("patientId")))==0)
		{
			session.setAttribute("Error", "PATIENT DOSENT HAVE ANY MATERIAL ALLOCATED");
			return false;
		}
		else
		{
			session.setAttribute("material", materials);
			session.setAttribute("patientId",  Integer.parseInt(request.getParameter("patientId")));
		}
		
		return true;
	}
	
	private boolean returnMaterials(HttpServletRequest request, HttpSession session) throws Exception
	{
		session.removeAttribute("Error");
		MaterialAllocation material=new MaterialAllocation();
    	material=(MaterialAllocation)session.getAttribute("material");
    	
    	CheckInOutDao.returnMaterials(material.getIAllocationId(), Integer.parseInt(session.getAttribute("patientId").toString()));
		
		
		
		return true;
		
	}
	
	private boolean allocateNewUtensils(HttpServletRequest request, HttpSession session) throws Exception
	{
		session.removeAttribute("Error");
		UtensilAllocation utensil= new UtensilAllocation();
    	
		utensil.setIAllocationId(CheckInOutDao.FindMaxutensilAllocationId()+1);
		utensil.setIPatientId(Integer.parseInt(request.getParameter("patientId")));
		utensil.setIAllocationType(Integer.parseInt(request.getParameter("settype")));
		utensil.setSAmountDeposited(Integer.parseInt(request.getParameter("amount")));
		utensil.setCalAllocationDate(request.getParameter("date").toString());
	
		if(CheckInOutDao.canPatientAllocateUtensil(utensil.getIPatientId())==false)
		{
			session.setAttribute("Error", "Patient Already Allocated Utensils");
			return false;
		}
		
    	CheckInOutDao.allocateNewUtensils(utensil);

		return true;
		
	}
	
	private boolean returnUtensils(HttpServletRequest request, HttpSession session) throws Exception
	{
		session.removeAttribute("Error");
		int patientId=Integer.parseInt(request.getParameter("patientId").toString());
		String date=request.getParameter("date").toString();
		
		if(CheckInOutDao.canPatientDeAllocateUtensil(patientId)==false)
		{
			com.bss.framework.FrontController.trace("------------>Patient Not Allowed to de allocate utensils");
			session.setAttribute("Error", "Patient Dosen't have any Allocated Utensils");
			return false;
		}
		else
		{
			com.bss.framework.FrontController.trace("------------>Patient  Allowed to de allocate utensils");
			session.setAttribute("utensil_amount", CheckInOutDao.findUtensilAmount(patientId));
		}
		
		CheckInOutDao.setUtensilDeallocateDate(date, patientId);
		
		return true;
		
	}
	
	private boolean checkinmain(HttpServletRequest request, HttpSession session) throws Exception
	{
		
		System.out.println("Checking in... 1"+new Date());
		int patientId;
		CheckInOut checkin=new CheckInOut();
		Patient patient=new Patient();
		System.out.println("Checkin 2");
		session.removeAttribute("Error");
		int addattendent1=0;
		int addattendent2=0;
		int addutensils=0;
		int addmaterials=0;
		System.out.println("Checkin 3");
		UtensilAllocation utensil= new UtensilAllocation();
		System.out.println("Checkin 4 "+new Date());
		MaterialAllocation materials=new MaterialAllocation();
		Attendent attendent1=new Attendent();
		Attendent attendent2=new Attendent();
		
		System.out.println("Checkin 5 "+request.getParameter("selectpatient")+","+request.getParameter("newpatient")+"," +new Date());
		System.out.println("Doing............");
		
		if(request.getParameter("selectpatient")!=null)
		{
			patientId=Integer.parseInt(request.getParameter("patientId").toString());
		}
		else if(request.getParameter("newpatient")!=null)
		{
			System.out.println("New patient...");
			patient.setSPatientFirstName(request.getParameter("firstname"));
			patient.setSPatientLastName(request.getParameter("lastname"));
			patient.setSPatientMiddleName(request.getParameter("middlename"));
			patient.setSAddress(request.getParameter("address"));
			patient.setSContactNo(request.getParameter("contactno"));
			patient.setSIdCardType(request.getParameter("idcardtype"));
			patient.setSIdCardNo(request.getParameter("idcardno"));
			patient.setSSex(request.getParameter("patientsex"));
			patient.setSMedicalProblem(request.getParameter("medical"));
			patient.setSProfession(request.getParameter("profession"));
			patient.setSTataCaseNo(request.getParameter("tatacaseno"));
			
			System.out.println("Adding new now...");
			
			patientId = PatientRecordDao.AddNewPatient(patient);
			
			System.out.println("uploading image");
			
			PatientRecordDao.updateLoadImage(patientId);
			
		}
		else
		{
			
			System.out.println("Cant add...");
			session.setAttribute("Error", "PATIENT ID NOT SET OR FOUND");
			return false;
		}
			
		checkin.setIPatientId(patientId);
		checkin.setINoOfChild(Integer.parseInt(request.getParameter("children")));
		checkin.setINoOfFemale(Integer.parseInt(request.getParameter("female")));
		checkin.setINoOfMale(Integer.parseInt(request.getParameter("male")));
		checkin.setCalCheckInTime(request.getParameter("date").toString());
		checkin.setSDateOfStay(request.getParameter("date1").toString());
		checkin.setSExtDateOfStay(request.getParameter("date2").toString());
		checkin.setSLastDepTown(request.getParameter("town").toString());
		checkin.setIDepositAmount(Integer.parseInt(request.getParameter("regamount")));
		
		
		
		
			attendent1.setIPatientId(patientId);
			
			attendent1.setSName(request.getParameter("name1").toString());
			attendent1.setSRelationship(request.getParameter("relationship1").toString());
			attendent1.setIAttendentUId(CheckInOutDao.FindMaxAttendentId()+1);
			attendent1.setSIdType(request.getParameter("idType1").toString());
			attendent1.setSIdNo(request.getParameter("idNo1").toString());
			addattendent1=1;
			
		
		if(request.getParameter("attendent2")!=null)
		{
			attendent2.setIPatientId(patientId);
			
			attendent2.setSName(request.getParameter("name2").toString());
			attendent2.setSRelationship(request.getParameter("relationship2").toString());
			attendent2.setIAttendentUId(CheckInOutDao.FindMaxAttendentId()+2);
			attendent2.setSIdType(request.getParameter("idType2").toString());
			attendent2.setSIdNo(request.getParameter("idNo2").toString());
			addattendent2=1;
			
		}
		
		
		if(request.getParameter("materials")!=null)
		{
			
			materials.setIAllocationId(CheckInOutDao.FindMaxMaterialAllocationId()+1);
			materials.setINoOfBS(Integer.parseInt(request.getParameter("bs")));
			materials.setINoOfWBS(Integer.parseInt(request.getParameter("dbs")));
			materials.setINoOfMN(Integer.parseInt(request.getParameter("mn")));
			materials.setINoOfDMN(Integer.parseInt(request.getParameter("dmn")));
			materials.setINoOfLK(Integer.parseInt(request.getParameter("lk")));
			materials.setINoOfMG(Integer.parseInt(request.getParameter("mg")));
			materials.setINoOfBK(Integer.parseInt(request.getParameter("bkt")));
			materials.setINoOfMlkTkt(Integer.parseInt(request.getParameter("mlktkt")));
			materials.setIOthersVal1(Integer.parseInt(request.getParameter("othersvalue1")));
			materials.setIOthersVal2(Integer.parseInt(request.getParameter("othersvalue2")));
			materials.setSOthersType1((request.getParameter("otherstype1")).toString());
			materials.setSOthersType2((request.getParameter("otherstype2")).toString());
			materials.setReturned(false);
			addmaterials=1;
			
		}
		
		if(request.getParameter("utensils")!=null)
		{
			utensil.setIAllocationId(CheckInOutDao.FindMaxutensilAllocationId()+1);
			utensil.setIPatientId(patientId);
			utensil.setIAllocationType(Integer.parseInt(request.getParameter("settype")));
			utensil.setSAmountDeposited(Integer.parseInt(request.getParameter("utensilamount")));
			utensil.setCalAllocationDate(request.getParameter("date").toString());
		
			if(CheckInOutDao.canPatientAllocateUtensil(utensil.getIPatientId())==false)
			{
				session.setAttribute("Error", "Patient Already Allocated Utensils");
				return false;
			}
			addutensils=1;
			
		}
		if(CheckInOutDao.checkin(checkin)==true)
		{
			int transactionId=CheckInOutDao.findLastTransactionId(patientId);
			if(request.getParameter("domroomId")!=null )
			{
				if(request.getParameter("domId").toString().length()!=0 )
				{
					String domId=request.getParameter("domId").toString();
					String roomId=request.getParameter("domroomId").toString();
					String domIds[]=domId.split(",");
					String roomIds[]=roomId.split(",");
					
					for(int i=0;i<domIds.length;i++)
					{
						com.bss.framework.FrontController.trace("domId:"+domIds[i]);
						RoomAllocation roomAllocation=new RoomAllocation();
						roomAllocation.setITransactionId(transactionId);
						roomAllocation.setIDomId(Integer.parseInt(domIds[i]));
						roomAllocation.setIRoomId(Integer.parseInt(roomIds[i]));
						roomAllocation.setINoOfDoms(domIds.length);
						roomAllocation.setIAmount(Integer.parseInt(request.getParameter("domamount").toString()));
						roomAllocation.setSDateOfStay(request.getParameter("date1").toString());
						RoomDao.allocateDom(roomAllocation);
						
					}
				}
				
			}
			
			
			if(request.getParameter("roomId")!=null)
			{
				if(request.getParameter("roomId").toString().length()!=0)
				{
					RoomAllocation roomAllocation=new RoomAllocation();
					roomAllocation.setITransactionId(transactionId);
					roomAllocation.setIRoomId(Integer.parseInt(request.getParameter("roomId").toString()));
					roomAllocation.setIAmount(Integer.parseInt(request.getParameter("roomamount").toString()));
					roomAllocation.setSDateOfStay(request.getParameter("date1").toString());
					RoomDao.allocateRoom(roomAllocation);
				}
			}
			
			if(addutensils==1)
			{
				CheckInOutDao.allocateNewUtensils(utensil);
			}
		
			if(addattendent1==1)
			{
				attendent1.setITransactionId(CheckInOutDao.findLastTransactionId(attendent1.getIPatientId()));
				CheckInOutDao.AddNewAttendent(attendent1);
				com.bss.framework.FrontController.trace("Adding attendent 1");
			}
			if(addattendent2==1)
			{
				attendent2.setITransactionId(CheckInOutDao.findLastTransactionId(attendent2.getIPatientId()));
				CheckInOutDao.AddNewAttendent(attendent2);
				com.bss.framework.FrontController.trace("Adding attendent 2");
			}
		
			if(addmaterials==1)
			{
				session.setAttribute("patientId", patientId);
				allocateMaterial(request, session);
			}
			
		}
		session.setAttribute("patientId", patientId);;
			return true;
		
	}
	private boolean checkoutmain(HttpServletRequest request, HttpSession session) throws Exception
	{
		//removing attributes
		session.removeAttribute("Error");
		session.removeAttribute("utensil_amount");
		//getting the selected patient id
		int patientId=Integer.parseInt(request.getParameter("patientId"));
		
		//finding the last transaction ID of patient 
		int transactionId=CheckInOutCommand.findLastTransactionId(patientId);
		
		String date=request.getParameter("date").toString();
		
			if(CheckInOutDao.canPatientDeAllocateUtensil(patientId)==false)
			{
				com.bss.framework.FrontController.trace("------------>Patient Not Allowed to de allocate utensils");		
			}
			else
			{
				com.bss.framework.FrontController.trace("------------>Patient  Allowed to de allocate utensils");
				CheckInOutDao.setUtensilDeallocateDate(date, patientId);
	    		com.bss.framework.FrontController.trace("Utnsil Deallocatd");
				session.setAttribute("utensil_amount", CheckInOutDao.findUtensilAmount(patientId));
			}
			
			MaterialAllocation materials=new MaterialAllocation();
			if(CheckInOutDao.selectMaterialsAllocated(materials, Integer.parseInt(request.getParameter("patientId")))==0)
			{
				session.setAttribute("Error", "PATIENT DOSENT HAVE ANY MATERIAL ALLOCATED");
			}
			else
			{
				CheckInOutDao.returnMaterials(materials.getIAllocationId(), Integer.parseInt(request.getParameter("patientId").toString()));
	    		com.bss.framework.FrontController.trace("Utnsil Deallocatd");
			}
		RoomDao.deallocateAllRooms(transactionId, date);
		RoomDao.tempCheckIn(transactionId);
		CheckInOutDao.setCheckoutDate(date, patientId);
		
		com.bss.framework.FrontController.trace(" CheckoutMain@Attendent Stay Modified count---: "+CheckInOutDao.makeIsPresentFalse(transactionId));
		
		return true;
	}
	public static MaterialAllocation getpatientmaterilas(int patientId) throws Exception
	{
		MaterialAllocation materials=new MaterialAllocation();
		if(CheckInOutDao.selectMaterialsAllocated(materials, patientId)==0)
		{
			return null;
		}
		else
		{
			return materials;
		}
		
	}
	
	public static int findLastTransactionId(int patientId) throws Exception
	{	
		return CheckInOutDao.findLastTransactionId(patientId);
	}
	
	public static CheckInOut loadCheckedInPatient(int transactionId)throws Exception
	{
		return CheckInOutDao.loadCheckedInPatient(transactionId);
	}
	public static UtensilAllocation PatientAllocatedUtensil(int patientId) throws Exception
	{
		return CheckInOutDao.PatientAllocatedUtensil(patientId);
	}
	
	public static ArrayList<Integer> findAllTransactionId(int patientId) throws Exception
	{
		return CheckInOutDao.findAllTransactionId(patientId);
	}
	
	private boolean findattendentstaylist(HttpServletRequest request, HttpSession session) throws Exception
	{
		session.removeAttribute("Error");
		int patientId=Integer.parseInt(request.getParameter("patientId"));
		int transactionId=CheckInOutCommand.findLastTransactionId(patientId);
		ArrayList<Attendent> attendents = CheckInOutDao.getAttendentList(transactionId);
		
		if(attendents.size() != 0 && attendents != null)
		{
			session.setAttribute("attendents", attendents);
		}
		else
		{
			com.bss.framework.FrontController.trace("Error");
			session.setAttribute("Error", "NO ATTENDENTS SELECTED");
			return false;
		}
		
		com.bss.framework.FrontController.trace("attendents size:"+attendents.size());
		return true;
	}
	
	private boolean updateattendentstaylist(HttpServletRequest request, HttpSession session) throws Exception
	{
		ArrayList<Attendent> attendents = (ArrayList<Attendent>)session.getAttribute("attendents");
		
		
		for(int i = 0; i < attendents.size(); i++)
		{
			if(attendents.get(i).getBIsPresent() == true && request.getParameter("stay"+i) == null)
			{
				CheckInOutDao.changeIsPresent(attendents.get(i).getIAttendentUId()	, false);
			}
			else if(attendents.get(i).getBIsPresent() == false && request.getParameter("stay"+i) != null)
			{
				CheckInOutDao.changeIsPresent(attendents.get(i).getIAttendentUId()	, true);
			}
		}
		session.removeAttribute("attendents");
		return true;
	}
	
	public static int findSecurityAmount(int patientId) throws Exception
	{
		return CheckInOutDao.findSecurityAmount(patientId);
	}
	
	public static int countNoOfPatientsIn() throws Exception
	{
		return CheckInOutDao.countNoOfPatientsIn();
	}
	
	public static int countNoOfAttendentsIn() throws Exception
	{
		return CheckInOutDao.countNoOfAttendentsIn();
	}
	
	public static ArrayList<Attendent> getAttendentList() throws Exception
	{
		return CheckInOutDao.getAttendentList();
	}
	
	public static String findDateOfStay(int transactionId) throws Exception
	{
		return CheckInOutDao.findDateOfStay(transactionId);
		
	}
}

